﻿#region "----Using Definitions----"

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data;

#endregion


#region "---Namespace_CXMLStudio.DataAccess---"

namespace CXMLStudioIDE.DataAccessLayer
{

    #region "---Abstract Class BaseDa---"

    public abstract class BaseDa
    {
	    private Database m_database = null;
	    private DbConnection dbConnection = null;
	    private DbTransaction dbTransaction = null;

        #region "---CreateDatabase Routine---"
        
        protected void CreateDatabase()
	    {
            //if value left blank then it uses default database in config file.
            //this.m_database = DatabaseFactory.CreateDatabase("GoldCXMLSQLConnect");
            this.m_database = DatabaseFactory.CreateDatabase("TestCXMLDB");
            //this.m_database = DatabaseFactory.CreateDatabase("Spectrum");
        }

        #endregion

        #region "---CreateConnection Routine---"

        protected void CreateConnection()
	    {
		    if (this.m_database == null) {
			    this.CreateDatabase();
		    }
		    this.dbConnection = this.m_database.CreateConnection();
        }

        #endregion
        
        #region "---BeginTransaction Function---"

        protected DbTransaction BeginTransaction()
	    {
		    if (this.dbConnection == null) {
			    this.CreateConnection();
		    }
		    if (dbConnection.State != ConnectionState.Open) {
			    this.dbConnection.Open();
		    }
		    this.dbTransaction = this.dbConnection.BeginTransaction();
		    return this.dbTransaction;
        }

        #endregion

        #region "---CommitTransaction Routine---"

        protected void CommitTransaction()
	    {
		    this.dbTransaction.Commit();
		    this.CloseConnection();
        }

        #endregion

        #region "---RollbackTransaction Routine---"

        protected void RollbackTransaction()
	    {
		    this.dbTransaction.Rollback();
		    this.CloseConnection();
        }

        #endregion

        #region "---CloseConnection Routine---"

        protected void CloseConnection()
	    {
		    this.dbConnection.Close();
        }

        #endregion

        #region "---OpenConnetion Function---"

        protected DbConnection OpenConnetion()
	    {
		    if (this.dbConnection == null) {
			    this.CreateConnection();
		    }
		    this.dbConnection.Open();
		    return this.dbConnection;
        }

        #endregion

        #region "---Database Function---"

        protected Database Database {
		    get { return this.m_database; }
		    set { this.m_database = value; }
        }

        #endregion

        #region "---Connection Function---"

        protected DbConnection Connection
        {
		    get { return this.dbConnection; }
		    set { this.dbConnection = value; }
        }

        #endregion

        #region "---Transaction Function---"

        protected DbTransaction Transaction {
		    get { return this.dbTransaction; }
		    set { this.dbTransaction = value; }
        }

        #endregion

    }

    #endregion

}

#endregion